package service.college;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import util.manager.PageManager;
import util.paramvo.SearchParamVO;
import util.returnvo.CollegeSearchListReturnVO;
import vo.CollegeVO;
import config.PageConfig;
import dao.college.CollegeDAO;

public class CollegeServiceImpl implements CollegeService{

	private CollegeDAO collegeDAO;

	public void setCollegeDAO(CollegeDAO collegeDAO) {
		this.collegeDAO = collegeDAO;
	}

	@Override
	public CollegeSearchListReturnVO getSearchedCollegeByWord(String search, String page) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}
		
		int pageNo=1;
		
		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);
		
		List<CollegeVO> collegelist=collegeDAO.getSearchedCollegeByWord(new SearchParamVO(searchlist, PageConfig.POPUP_ROW_PER_PAGE, pageNo));
		
		return new CollegeSearchListReturnVO(collegelist, new PageManager(PageConfig.POPUP_ROW_PER_PAGE, PageConfig.POPUP_PAGE_PER_GROUP, collegeDAO.countSearchedCollegeByWord(searchlist), pageNo));
	}

	@Override
	public List<CollegeVO> getCollegeEditList(String search) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<CollegeVO>();
		
		return collegeDAO.getSearchedCollegeByWord(new SearchParamVO(searchlist, PageConfig.EDIT_ROW_PER_PAGE, 1));
	}

	@Override
	public List<String> autoCompleteCollegeList(String search) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<String>();
		
		List<CollegeVO> collegelist=collegeDAO.getSearchedCollegeByWord(new SearchParamVO(searchlist, PageConfig.AUTOCOMPL_ROW_PER_PAGE, 1));
		
		ArrayList<String> returnlist=new ArrayList<String>();
		
		for(CollegeVO vo : collegelist){
			returnlist.add(vo.getCollege_name());
		}
		
		return returnlist;
	}

	@Override
	public void registerCollege(String college_name) throws Exception {
		collegeDAO.registerCollege(college_name);
	}

	@Override
	public void updateCollege(CollegeVO param) throws Exception {
		collegeDAO.updateCollege(param);
	}

	@Override
	public void deleteCollege(int college_no) throws Exception {
		collegeDAO.deleteCollege(college_no);
	}
}
